import salespricingsApi from "../../../common/api/breedsales/salespricings.api.js"

import doT from '../../../../vendor/template/doT.js';
import { requestFilePath } from "../../../common/api/file-request.js";
import watermark from '../../../common/watermark.js';
import { requestDict, DictionaryType } from '../../../common/dictionary/dictionary-type.js';

(async function (window, document, $) {
    let _pigTypeList = [], listindex = "";
    // 获取参数
    const _json = Utils.ParsingUrl(window.location);
    const looksalesTemplate = doT.template($('#looksalesplan-template').text());
    const lksalesmainTemplate = doT.template($('#looksalesplanmain-template').text());
    $(function () {
        $('body').attr('data-weui-theme', 'light'); //强制光明模式
        _watermarkBgImg();
    });
    // 定价类型
    requestDict(DictionaryType.PIG_FIXEDPRICE_TYPE).then(res => {
        _pigTypeList = res.data;
        _initCommand()
    });
    //返回列表
    $(document).on('click', '#returnlist', function () {
        location.href = `../salespricings/salespricingslist.html?v=${Utils.GetTime()}`;
    });

    //切换导航栏
    $('#spleplan-navbar a').on('click', function () {
        listindex = Number($(this).index());
        $(this).addClass('weui-bar__item--on').siblings().removeClass('weui-bar__item--on');
        if (listindex === 0) { //销售出库
            listindex = ""
            _initCommand()
        }
    });

    //命令初始化
    const _initCommand = async () => {
        let ids = getUrlKey("id")
        salespricingsApi.GetSalesPricings(ids).then(res => {
            res.pigTypeName = _pigTypeList.find(s => s.value == res.pigType)?.displayName || ''
            res.entries.map(item => {
                item.bTypeName = item.bType == 1 ? 'A票' : item.bType == 2 ? 'B票' : ''
            })
            console.log(res.entries)
            $('#looksalesMain').empty().append(lksalesmainTemplate(res));
            $('#looksalesEntry').empty().append(looksalesTemplate({ entreis: res.entries }));
        });
    }

    /** 获取地址栏的 参数  传入 字段返回值 */
    const getUrlKey = (name) => {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    // 添加水印信息
    const _watermarkBgImg = () => {
        let _userInfo = JSON.parse(Utils.GetSessionStorage("SNAccountEply")) || {};
        watermark.set(_userInfo.name + _userInfo.numCode);// 水印
    }

})(window, document, jQuery)